package com.sx.pipeline.adapter

import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.sx.pipeline.R
import com.sx.pipeline.repos.http.res.CommonApplyListVO

/**
 * @Author: YY
 * @Date: 2022/5/19
 * @Description: TODO
 */
class HomeApplyAdapter(
    private val applyList: List<CommonApplyListVO>,
    private val rowMaxCount: Int,
): PagerAdapter() {

    override fun getCount(): Int {
        val size: Int = applyList.size
        if (size < rowMaxCount) {
            return 1
        }
        return if (size % rowMaxCount == 0) {
            size / rowMaxCount
        } else {
            size / rowMaxCount + 1
        }
    }

    private lateinit var itemClickListener: ItemClickListener

    fun setItemClickListener(listener: ItemClickListener) {
        this.itemClickListener = listener
    }

    interface ItemClickListener {
        fun onClickItem(position: Int, itemData: CommonApplyListVO)
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val recyclerView = RecyclerView(container.context)
        recyclerView.layoutManager = GridLayoutManager(container.context, rowMaxCount)
        val startIndex: Int = position * rowMaxCount
        val endIndex = if (position == count - 1) {
            applyList.size
        } else {
            startIndex + rowMaxCount
        }
        val toolsUiData: List<CommonApplyListVO> = this.applyList.subList(startIndex, endIndex)
        val homeIconAdapter = HomeIconAdapter(toolsUiData as MutableList<CommonApplyListVO>)
        recyclerView.adapter = homeIconAdapter
        homeIconAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                if (this@HomeApplyAdapter::itemClickListener.isInitialized) {
                    val data = adapter.data[position] as CommonApplyListVO
                    itemClickListener.onClickItem(position, data)
                }
            }
        })

        container.addView(recyclerView)
        return recyclerView
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }


    class HomeIconAdapter(dataList: MutableList<CommonApplyListVO>)
        : BaseQuickAdapter<CommonApplyListVO, BaseViewHolder>(R.layout.item_home_icon, dataList) {

        override fun convert(holder: BaseViewHolder, item: CommonApplyListVO) {
            if (!TextUtils.isEmpty(item.iconUrl)) {
                Glide.with(context)
                    .load(item.iconUrl)
                    .error(R.drawable.ic_mine_release)
                    .into(holder.getView(R.id.image_icon))
            }
            if (!TextUtils.isEmpty(item.name)) {
                holder.setText(R.id.tv_name, item.name)
            }
            if(item.num==null||item.num==0){
                holder.getView<TextView>(R.id.tv_unread_num).visibility=View.GONE
            }else{
                holder.getView<TextView>(R.id.tv_unread_num).visibility=View.VISIBLE
                holder.setText(R.id.tv_unread_num,item.num.toString())
            }
        }
    }
}